package com.chris.dubboconsumer;


import java.io.IOException;

        import org.apache.zookeeper.CreateMode;
        import org.apache.zookeeper.KeeperException;
        import org.apache.zookeeper.WatchedEvent;
        import org.apache.zookeeper.Watcher;
        import org.apache.zookeeper.ZooDefs;
        import org.apache.zookeeper.ZooKeeper;
        import org.apache.zookeeper.data.Stat;

/**
 * 测试单机Zookeeper环境连接
 * @author chaozai
 * @date 2019年3月5日
 *
 */
public class zookeeperTest {

    public static void main(String[] args) {
        try {
            ZooKeeper zk = new ZooKeeper("192.168.117.130:2181", 10000, new TestWatcher());
            String node = "/test";
            Stat stat = zk.exists(node, false);
            //如果节点不存在，则创建节点
            if(null==stat){
                String createResult = zk.create(node, "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                //获取创建结果
                System.out.println(createResult);
            }
            //将会把“test”字符返回
            byte[] b = zk.getData(node, false, stat);
            System.out.println(new String(b));
            zk.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
/**
 * 测试Watcher
 * @author chaozai
 * @date 2019年3月5日
 *
 */
class TestWatcher implements Watcher{

    public void process(WatchedEvent arg0) {
        System.out.println("-----------------------");
        System.out.println("WatchedEvent:"+arg0);
        System.out.println("-----------------------");
    }

}